# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：PyJWT_model.py
#日期：2023-12-9
#备注：Python中 生成和校验 JWT 令牌    
# pip install python-jose / pip install PyJWT 
pip install --upgrade -i https://mirrors.aliyun.com/pypi/simple/ cryptography
https://github.com/mpdavis/python-jose
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import jwt
from pycacho.cachobase.file_deal import get_file
from pycacho.cachobase.deal_encrypt import generate_key


algorithm = "RS256"
encoded_jwt = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImxpbmsiOiJodHRwOi8vbXAud2VpeGluLnFxLmNvbS9zP19fYml6PU1qTTVOakF4T1RVNE1BPT0mYW1wO21pZD0zMDA5MjE3NTkwJmFtcDtpZHg9MSZhbXA7c249MTQ1MzJjNDliYzhjYjA4MTc1NDQxODFhMTBlOTMwOWYmYW1wO2Noa3NtPTkwNDYwODI1YTczMTgxMzNlNzkwNWMwMmU3MDhkNTIyMmFiZmVhOTMwZTYxYjQyMTZmMTViNzUwNGUzOTczNGJjZDQxY2ZiMGEyNmQmYW1wO3NjZW5lPTI3I3dlY2hhdF9yZWRpcmVjdCIsImlhdCI6MTUxNjIzOTAyMn0.Q3T9O4B9IzoZLxcGPgeLMg1qQXKsDONeaymtr8QeLBmeCTm9ToVM0IufhpEcrn-QLxTwLfE1S90hg90T3R1F6ZwNOgq_o5XG75Lj7Gb3Mj_jNSYssO0XrozEvj6lQFAy67SBcuqKtVsuMTcvb9OECrwE600OtbDGXejugGWE_21x2QbFCHGSTHn_JrgEZCblw0OAI11kqo0nJCGulIsNbyO85uqSMz8LeBtxVF120UXvP-oxPwVEPTIGAiEcnuAC2E9ISBeZyeO79-wFTFox8VW2yCRls4RELwVhDupgujxxTGWPY8r0tX3Wrt2Q4BNWiTxCIocOhGN-2HitLwJlSQ"

header = {
  "alg": "RS256",
  "typ": "JWT"
}

payload = {
  "sub": "1234567890",
  "name": "John Doe",
  "admin": True,
  "link":"http://mp.weixin.qq.com/s?__biz=MjM5NjAxOTU4MA==&amp;mid=3009217590&amp;idx=1&amp;sn=14532c49bc8cb0817544181a10e9309f&amp;chksm=90460825a7318133e7905c02e708d5222abfea930e61b4216f15b7504e39734bcd41cfb0a26d&amp;scene=27#wechat_redirect",
  "iat": 1516239022
}

def get_private_key():
    private_key = get_file("private_key.pem")
    #print(private_key)
    return private_key

def get_public_key():
    private_key = get_file("public_key.pem")
    #print(private_key)
    return private_key

def jwt_token():
    jwt_encode = jwt.encode(payload,get_private_key(),algorithm='RS256',headers=header)
    jwt_decode = jwt.decode(encoded_jwt, get_public_key(), algorithms=['RS256'])
    print("加密后的token: ",jwt_encode)
    print("解密后的数据: ", jwt_decode)

if __name__ == "__main__":
    #generate_key("private_key.pem","public_key.pem")
    jwt_token()

